package action;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.connector.Response;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import dao.Manages;
import dao.ManagesDao;

public class AdminLeftManages extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List adminLeftM;
	private ManagesDao managesDao;
	private List adminLeft_chM;
	public List getChManages(int pid){
		adminLeft_chM=managesDao.findByProperty("belongs", pid);
		return adminLeft_chM;
	}
	public String execute(){
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8");
		adminLeftM=managesDao.findByProperty("belongs", 0);
		Logger log = Logger.getLogger(ManagesDao.class.getName());
		
		String menu="[";
		for(int i = 0 ; i< adminLeftM.size();i++){    
 
			Manages manage = (Manages) adminLeftM.get(i);   
			//log.info(manage.getTitle() + " : " + manage.getUrlPage() + " : " + manage.getId());
			menu += "{\"text\":\""+manage.getTitle()+"\",\"state\":\"closed\",\"attributes\":{\"url\":\""+manage.getUrlPage()+"\"},\"children\":[";  
			adminLeft_chM = managesDao.findByProperty("belongs", manage.getId());
			log.info(adminLeft_chM.size());
			for(int j=0;j<adminLeft_chM.size();j++){
				Manages manage_chM = (Manages) adminLeft_chM.get(j);
				menu +="{\"id\":\"j"+j+"\",\"text\":\""+manage_chM.getTitle()+"\",\"attributes\":{\"url\":\""+manage_chM.getUrlPage()+"\"}}";
				if(j<(adminLeft_chM.size()-1)){
					menu +=",";
				}
			}
			menu +="]}"; 
			if(i<adminLeftM.size()-1){
				menu +=",";
			}
		}
		menu +="]";
		
		try {
			/*response.getWriter().println("[{\"text\":\"Languages\",\"state\":\"closed\",\"attributes\":{\"url\":\"\"}," +
					"\"children\":[{\"id\":\"j1\",\"text\":\"Java\",\"attributes\":{\"url\":\"/demo/book/Java\"}}," +
					"{\"id\":\"j2\",\"text\":\"C#\",\"attributes\":{\"url\":\"/demo/book/C#\"}}," +
					"{\"id\":\"j3\",\"text\":\"PHP\",\"attributes\":{\"url\":\"/demo/book/PHP\"}}," +
					"{\"id\":\"j3\",\"text\":\"ASP\",\"attributes\":{\"url\":\"/demo/book/ASP\"}}]}]");*/
			response.getWriter().println(menu);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public ManagesDao getManagesDao(){
		return managesDao;
	}
	public void setManagesDao(ManagesDao managesDao){
		this.managesDao=managesDao;
	}
	public List getAdminLeftM() {
		return adminLeftM;
	}
	public void setAdminLeftM(List adminLeftM) {
		this.adminLeftM = adminLeftM;
	}
}
